'''双向链表'''


class DoubleNode(object):
    def __init__(self, item):
        ''' 双向链表节点 '''
        self.item = item
        self.prev = None
        self.next = None


class DoubleLinkList(object):
    def __init__(self, item=None):
        node = self.__get_node(item)
        self.__head = node

    def __get_node(self, o):
        if o is None:
            return None
        elif isinstance(o, DoubleNode):
            return o
        else:
            return DoubleNode(o)

    def is_empty(self):
        return self.__head is None

    def length(self):
        count = 0
        current = self.__head
        while current:
            count += 1
            current = current.next
        return count

    def travel(self):
        current = self.__head
        print('strat')
        while current:
            print(current.item)
            current = current.next
        print('end')

    def add(self, item):
        node = self.__get_node(item)
        if self.__head is None:
            self.__head = node
        else:
            node.next = self.__head
            self.__head.prev = node
            self.__head = node

    def append(self, item):
        node = self.__get_node(item)
        if self.__head is None:
            self.__head = node
        else:
            current = self.__head
            while current.next:
                current = current.next
            current.next = node
            node.prev = current

    def insert(self, pos, item):
        node = self.__get_node(item)
        current = self.__head
        if current is None:
            self.__head = node
        elif pos <= 0:
            current.prev = node
            node.next = current
            self.__head = node
        else:
            count = 0
            while current.next:
                count += 1
                if count == pos:
                    node.prev = current
                    node.next = current.next
                    current.next = node
                    return
                else:
                    current = current.next
            if pos > count:
                current.next = node
                node.prev = current

    def remove(self, item):
        if self.__head is None:
            return
        current = self.__head
        while current.next:
            if current.item == item:
                if current.prev:
                    current.prev.next = current.next
                else:
                    next_node = current.next
                    next_node.prev = None
                    self.__head = next_node
                return
            else:
                current = current.next

        if current.item == item:
            if current.prev:
                current.prev.next = None
            else:
                self.__head = None

    def search(self, item):
        if self.__head is None:
            return False
        current = self.__head
        while current:
            if current.item == item:
                return True
            else:
                current = current.next
        return False


if __name__ == '__main__':
    dl = DoubleLinkList()
    dl.insert(-9, 0)

    dl.add(12)
    dl.append(1)
    dl.remove(1)
    dl.insert(1, 182)
    dl.add(43)
    dl.remove(43)
    dl.travel()
